﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.Utilities.Excel.Validator
{
    public class RegexUtil
    {
        /// <summary>
        /// 正则验证,字符为空时返回 true ; 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsRegexPattern(string value, string pattern)
        {
            if (string.IsNullOrEmpty(value))
            {
                return true;
            }

            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);

            return regex.IsMatch(value);
        }

        /// <summary>
        /// 空值验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsRequried(string value)
        {
            return !string.IsNullOrEmpty(value);
        }

        /// <summary>
        /// 网址验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsUrl(string value)
        {
            string pattern = @"^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// QQ号码验证（非零开头的四位以上的数字）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsQQ(string value)
        {
            string pattern = @"^[1-9][0-9]{4,}$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 电话号码验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsTelephone(string value)
        {
            string pattern = @"^(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 手机号码验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsMobile(string value)
        {
            string pattern = @"^1[3|5|8]\d{9}$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 邮政编码验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsZip(string value)
        {
            string pattern = @"^[1-9]\d{5}$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 邮箱地址验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsEmail(string value)
        {
            string pattern = @"^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$";

            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 日期格式验证（YYYY-MM-DD）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDate(string value)
        {
            string pattern = @"^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 身份证验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsIdentity(string value)
        {
            string pattern = @"\d{15}|\d{18}";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 金额格式验证
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsMoney(string value)
        {
            string pattern = @"^[0-9]+(.[0-9]{1,2})?$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 输入值必须是正整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsInteger(string value)
        {
            string pattern = @"^\d+$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 输入值必须是数值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool isDouble(string value)
        {
            string pattern = @"^[0-9]+(.[0-9]{1,})?$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 只能输入数字
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsDigit(string value)
        {
            string pattern = @"^[0-9]+$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 不允许输入字母和数字之外的特殊字符
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNoSpecialCaracters(string value)
        {
            string pattern = @"^[0-9a-zA-Z]+$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 只允许输入英文
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsLetter(string value)
        {
            string pattern = @"^[a-zA-Z]+$";
            return IsRegexPattern(value, pattern);
        }

        /// <summary>
        /// 只允许输入中文
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsChinese(string value)
        {
            string pattern = @"^^[\u0391-\uFFE5]+$";
            return IsRegexPattern(value, pattern);
        }
    }
}
